package com.mystore.framework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mystore.framework.mapper.AppMapper;
import com.mystore.framework.pojo.App;
import com.mystore.framework.pojo.AppModel;
import com.mystore.framework.pojo.Role;
import com.mystore.framework.pojo.User;
import icu.mhb.mybatisplus.plugln.base.mapper.JoinBaseMapper;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import org.mybatis.spring.transaction.SpringManagedTransaction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class IAppServiceImpl extends IBaseServiceImpl<AppMapper,App>{
    @Transactional
    public int insertOne(App t) {
        if(!StringUtils.hasText(t.getAppIcon()))
            t.setAppIcon(null);
        if(!StringUtils.hasText(t.getAppImage()))
            t.setAppImage(null);
        return baseMapper.insert(t)==0?0:t.getAppId();
    }
    @Transactional
    public boolean updateOne(App t) {
        UpdateWrapper<App> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq(true,"app_id",t.getAppId());
        if(!StringUtils.hasText(t.getAppIcon()))
            t.setAppIcon(null);
        if(!StringUtils.hasText(t.getAppImage())){
            updateWrapper.set("app_image",null);
        }
        return baseMapper.update(t,updateWrapper)>0;
    }
    @Override
    public App selectById(int id) {
        JoinLambdaWrapper<App> queryWrapper=new JoinLambdaWrapper<>(App.class);
        queryWrapper.leftJoin(AppModel.class,AppModel::getAppId,App::getAppId)
                .manyToManySelect(App::getAppModelList,AppModel.class).end();
        QueryWrapper<App> appQueryWrapper=new QueryWrapper<>();
        appQueryWrapper.eq("app_id",id);
        queryWrapper.changeQueryWrapper(appQueryWrapper);
        return baseMapper.joinSelectOne(queryWrapper,App.class);
    }

    @Override
    public List<App> selectAll() {
        JoinLambdaWrapper<App> queryWrapper=new JoinLambdaWrapper<>(App.class);
        queryWrapper.leftJoin(AppModel.class,AppModel::getAppId,App::getAppId)
                .manyToManySelect(App::getAppModelList,AppModel.class).end();
        return baseMapper.joinSelectList(queryWrapper,App.class);
    }

    @Override
    public IPage<App> selectPage(Page<App> page) {
        JoinLambdaWrapper<App> queryWrapper=new JoinLambdaWrapper<>(App.class);
        queryWrapper.leftJoin(AppModel.class,AppModel::getAppId,App::getAppId)
                .manyToManySelect(App::getAppModelList,AppModel.class).end();
        return baseMapper.joinSelectPage(page,queryWrapper,App.class);
    }

    @Override
    public IPage<App> selectPageByData(String data, Page<App> page) {
        JoinLambdaWrapper<App> queryWrapper=new JoinLambdaWrapper<>(App.class);
        queryWrapper.leftJoin(AppModel.class,AppModel::getAppId,App::getAppId)
                .manyToManySelect(App::getAppModelList,AppModel.class)
                .apply("(CONCAT_WS('-',tb_app.app_name,tb_app.app_description)) like {0}","%"+data+"%")
                .end();
        return baseMapper.joinSelectPage(page,queryWrapper,App.class);
    }
    public IPage<App> selectOnlinePageByData(String data, Page<App> page) {
        JoinLambdaWrapper<App> queryWrapper=new JoinLambdaWrapper<>(App.class);
        queryWrapper.leftJoin(AppModel.class,AppModel::getAppId,App::getAppId)
                .manyToManySelect(App::getAppModelList,AppModel.class)
                .apply("(CONCAT_WS('-',tb_app.app_name,tb_app.app_description)) like {0}","%"+data+"%")
                .end();
        QueryWrapper<App> appQueryWrapper=new QueryWrapper<>();
        appQueryWrapper.eq("app_state",0);
        queryWrapper.changeQueryWrapper(appQueryWrapper);
        return baseMapper.joinSelectPage(page,queryWrapper,App.class);
    }
    public IPage<App> selectOfflinePageByData(String data, Page<App> page) {
        JoinLambdaWrapper<App> queryWrapper=new JoinLambdaWrapper<>(App.class);
        queryWrapper.leftJoin(AppModel.class,AppModel::getAppId,App::getAppId)
                .manyToManySelect(App::getAppModelList,AppModel.class)
                .apply("(CONCAT_WS('-',tb_app.app_name,tb_app.app_description)) like {0}","%"+data+"%")
                .end();
        QueryWrapper<App> appQueryWrapper=new QueryWrapper<>();
        appQueryWrapper.eq("app_state",1);
        queryWrapper.changeQueryWrapper(appQueryWrapper);
        return baseMapper.joinSelectPage(page,queryWrapper,App.class);
    }
    public List<App> selectTop3(){
        JoinLambdaWrapper<App> queryWrapper=new JoinLambdaWrapper<>(App.class);
        queryWrapper.leftJoin(AppModel.class,AppModel::getAppId,App::getAppId)
                .manyToManySelect(App::getAppModelList,AppModel.class).end();
        QueryWrapper<App> appQueryWrapper=new QueryWrapper<>();
        appQueryWrapper.eq("app_state",0);
        appQueryWrapper.orderByDesc("download_count");
        queryWrapper.changeQueryWrapper(appQueryWrapper);
        return joinPage(new Page<App>(1,3),queryWrapper,App.class).getRecords();
    }
    public boolean offlineApp(int appId){
        App app=this.selectById(appId);
        if (app==null)
            return false;
        else if(app.getAppState()==null||app.getAppState()==1)
            return false;
        else if (app.getAppState()==0) {
            app.setAppState(1);
            return this.updateOne(app);
        }else
            return false;
    }
    public boolean onlineApp(int appId){
        App app=this.selectById(appId);
        if (app==null)
            return false;
        else if(app.getAppState()==null||app.getAppState()==0)
            return false;
        else if (app.getAppState()==1) {
            app.setAppState(0);
            return this.updateOne(app);
        }else
            return false;
    }
}
